package com.seamtop.cuber.server.controller;

import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.entriy.CuberReducerJob;
import com.seamtop.cuber.common.entriy.Duration;
import com.seamtop.cuber.common.util.DateUtil;
import com.seamtop.cuber.server.job.JobKits;
import com.seamtop.cuber.server.service.CuberInitService;
import com.seamtop.cuber.server.service.CuberJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by feng on 2015/8/21.
 */
@Controller("CuberJobController")
@RequestMapping("cuberjob")
public class CuberJobController {

    private static final Logger LOG = LoggerFactory.getLogger(CuberJobController.class);

    @Autowired
    private CuberJobService cuberJobService;

    @RequestMapping("/execute.action")
    public String execute(HttpServletRequest request, HttpServletResponse response,
                          ModelMap model) {
        String period = request.getParameter("period");
        String reducerName = request.getParameter("reducer");
        Date date = DateUtil.transStrToDate(period);
        System.out.println("date:"+date);
        long t1 = date.getTime();
        long t2 = DateUtil.getAheadDayTimeStamp(date,1);
        System.out.println("t1:"+t1);
        System.out.println("t2:"+t2);
        Duration duration = new Duration();
        duration.setStartTime(t1);
        duration.setEndTime(t2);
        HashMap<String,CuberReducer> cuberReducerMap = DataObject.getCuberReducerMap();
        CuberReducer reducer = cuberReducerMap.get(reducerName);
        CuberReducerJob cuberReducerJob = new CuberReducerJob();
        cuberReducerJob.setReducer(reducer);
        cuberReducerJob.setDuration(duration);
        try{
            cuberJobService.execute(cuberReducerJob);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
